<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
     
      <el-form-item label="订单编号" prop="orderNo">
        <el-input
          v-model="queryParams.orderNo"
          placeholder="请输入订单编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单信息" prop="attribute">
        <el-input
          v-model="queryParams.attribute"
          placeholder="请输入订单信息"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
     
      <el-form-item label="退款人" prop="refundName">
        <el-input
          v-model="queryParams.refundName"
          placeholder="请输入退款人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--     
      <el-form-item label="退款类型" prop="reasonType">
        <el-select v-model="queryParams.reasonType" placeholder="请选择退款类型" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item> -->
      <el-form-item label="退款原因" prop="reason">
        <el-input
          v-model="queryParams.reason"
          placeholder="请输入退款原因"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
    
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
     
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="refundList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="订单编号" align="center" prop="orderNo" width="170" />
      <el-table-column label="订单信息" align="left" prop="attribute" width="170" :show-overflow-tooltip="true" />
      <el-table-column label="订单金额" align="center" prop="orderAmount" />
      <el-table-column label="退款人" align="center" prop="refundName" />
      <el-table-column label="退款人电话" align="center" prop="refundPhone" />
      <el-table-column label="退款金额" align="center" prop="refundAmount" />
      <el-table-column label="退款类型" align="center" prop="reasonType" >
        <template slot-scope="scope">
          <span v-if="scope.row.reasonType == 0">个人原因</span>
          <span v-if="scope.row.reasonType == 2">审核通过</span>
        </template>
      </el-table-column>
      <el-table-column label="退款原因" align="center" prop="reason" />
      <el-table-column label="图片" align="center" prop="imgUrl" />
      <el-table-column label="状态" align="center" prop="status" >
        <template slot-scope="scope">
          <span v-if="scope.row.status == 1">待审核</span>
          <span v-if="scope.row.status == 2">审核通过</span>
          <span v-if="scope.row.status == 3">拒绝</span>
          <span v-if="scope.row.status == 4">已退款</span>
        </template>
      </el-table-column>
     <el-table-column label="申请时间" align="center" prop="createTime" width="150">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="100" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit" v-if="scope.row.status == 1"
            @click="handleApprove(scope.row)"
          >审核</el-button>
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-refresh-left" v-if="scope.row.status == 2"
            @click="handleRefund(scope.row)"
          >确认退款</el-button> -->
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改退款记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="50%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px" :disabled="true">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="订单编号" prop="orderNo">
              <el-input v-model="form.orderNo" placeholder="请输入订单编号" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="订单信息" prop="attribute">
              <el-input v-model="form.attribute" placeholder="请输入订单信息" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="订单金额" prop="orderAmount">
              <el-input v-model="form.orderAmount" placeholder="请输入订单金额" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="退款金额" prop="refundAmount">
              <el-input v-model="form.refundAmount" placeholder="请输入退款金额" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="退款人" prop="refundName">
              <el-input v-model="form.refundName" placeholder="请输入退款人" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="退款人电话" prop="refundPhone">
              <el-input v-model="form.refundPhone" placeholder="请输入退款人电话" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="退款类型" prop="reasonType">
          <el-select v-model="form.reasonType" placeholder="请选择退款类型">
            <el-option label="请选择字典生成" value="" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="退款原因" prop="reason">
              <el-input v-model="form.reason" type="textarea" placeholder="请输入退款原因" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="图片" prop="imgUrl">
              <el-input v-model="form.imgUrl" placeholder="请输入图片" />
            </el-form-item>     
          </el-col>
          <el-col :span="12">
            
          </el-col>
        </el-row>
       
        
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="success" @click="submitPass" v-if="approved">通 过</el-button>
        <el-button type="warning" @click="submitNotPass" v-if="approved">驳 回</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRefund, getRefund, approveRefund, refund } from "@/api/order/refund";

export default {
  name: "Refund",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      approved: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 退款记录表格数据
      refundList: [],
      payTypeOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderId: null,
        orderAmount: null,
        orderNo: null,
        attribute: null,
        refundUserId: null,
        refundName: null,
        refundPhone: null,
        reasonType: null,
        reason: null,
        refundAmount: null,
        imgUrl: null,
        status: null,
        approveTime: null,
        refundTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("tb_payment_type").then(response => {
      this.payTypeOptions = response.data;
    });
  },
  methods: {
    /** 查询退款记录列表 */
    getList() {
      this.loading = true;
      listRefund(this.queryParams).then(response => {
        this.refundList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 字典状态字典翻译
    formatPaytype(row, column) {
      return this.selectDictLabel(this.payTypeOptions, row.payType);
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        orderId: null,
        orderAmount: null,
        orderNo: null,
        attribute: null,
        refundUserId: null,
        refundName: null,
        refundPhone: null,
        reasonType: null,
        reason: null,
        refundAmount: null,
        imgUrl: null,
        createTime: null,
        status: "0",
        approveTime: null,
        refundTime: null
      };
      this.resetForm("form");
      this.approved = false;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加退款记录";
    },
    /** 修改按钮操作 */
    handleApprove(row) {
      this.reset();
      this.approved = true;
      const id = row.id || this.ids
      getRefund(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "退款记录";
      });
    },
    /** 提交按钮 */
    submitPass: function() {
      this.form.status = 2;
      approveRefund(this.form).then(response => {
        this.msgSuccess("操作成功");
        this.open = false;
        this.getList();
      });
    },
     /** 提交按钮 */
    submitNotPass: function(){
      this.form.status = 3;
      approveRefund(this.form).then(response => {
        this.msgSuccess("操作成功");
        this.open = false;
        this.getList();
      });
    },
    handleRefund(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认已打款?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return refund(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("操作成功");
        })
    }
  }
};
</script>
